package cn.java86;
//import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author oldliu
 * @since 1.0
 */
public class Jdbc1 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        System.out.println("找到驱动");
        //?参数
        String url = "jdbc:mysql://localhost:3308/java86school?useUnicode=true&characterEncoding=utf8&useSSL=false" +
                "&serverTimezone=GMT%2B8";
        String user = "root";
        String pass = "root1234";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, user, pass);
            //
            System.out.println("接口的实现类：" + con.getClass().getName());
            st = con.createStatement();
            rs = st.executeQuery("select * from s_subject order by id desc limit 0,10");
            //第一行之前
            while (rs.next()) {
                //通过列名取结果
                long id = rs.getLong("id");
                String sname = rs.getString("subjectname");
                int hours = rs.getInt("classhour");
                long gradeid = rs.getLong("gradeid");
                System.out.println(id + " " + sname + " " + hours + " " + gradeid);
            }
            // System.in.read();
//            rs.close();
//            st.close();
//            con.close();
            System.out.println("执行完毕");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                rs.close();
            } catch (Exception e) {
                //e.printStackTrace();
            }
            try {
                if (st != null)
                    st.close();
            } catch (Exception e) {
                //e.printStackTrace();
            }
            try {
                con.close();
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }
}
